package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.requestpage.OrderMessagePage;
import cn.com.api.service.ApiTOrderMessageService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "orderMessage/")
public class ApiTOrderMessageController {
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiTOrderMessageService apiTOrderMessageService;

	private static Logger logger = Logger
			.getLogger(ApiTOrderMessageController.class);

	/**
	 * 提醒接单--给商家推送消息+存库(显示到商家催单里)
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "remindTakeOrder")
	public void remindTakeOrder(OrderMessagePage orderMessagePage,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = orderMessagePage.getU_id();
		String token = orderMessagePage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map)
					&& checkService
							.checkOrderMessagePage(orderMessagePage, map)) {
				apiTOrderMessageService.remindTakeOrder(orderMessagePage, map);
			}

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 提醒发货--给商家推送消息+存库(显示到商家催单里)
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "remindSendGoods")
	public void remindSendGoods(OrderMessagePage orderMessagePage,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = orderMessagePage.getU_id();
		String token = orderMessagePage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map)
					&& checkService
							.checkOrderMessagePage(orderMessagePage, map)) {
				apiTOrderMessageService.remindSendGoods(orderMessagePage, map);
			}

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 提醒配送员取衣服
	 * @param orderMessagePage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "remindSenderForClothes")
	public void remindSenderForClothes(HttpServletRequest request, HttpServletResponse response)throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String shop_id = request.getParameter("shop_id");
			String token = request.getParameter("token");
			String orderId = request.getParameter("orderId");
			if (checkService.checkToken(shop_id, token, map)){
				apiTOrderMessageService.remindSenderForClothes(shop_id, orderId);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

}
